﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ScoreController : ControllerBase
    {
        private readonly IScoreService _service;

        private readonly IMapper _mapper;

        public ScoreController(IScoreService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        [HttpGet]
        public async Task<IActionResult> GetScores(string? studentName, string? courseName, int pageNum, int pageSize)
        {
            var data = await _service.GetScores(studentName, courseName, pageNum, pageSize);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetScore(int id)
        {
            var data = await _service.GetEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpPost]
        public async Task<IActionResult> AddScore(ScoreDTO s)
        {
            var score = _mapper.Map<Score>(s);
            var data = await _service.AddEntity(score);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateScore(ScoreDTO s)
        {
            var score = _mapper.Map<Score>(s);
            var data = await _service.UpdateEntity(score);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteScore(int id)
        {
            var data = await _service.DeleteEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }
    }
}
